﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Elysium.Demo;
using SIIE_ERP.Codigo;
using SIIE_ERP.View;
using SIIE_ERP.BaseDatos;
using Elysium.Demo.Controls;

namespace Elysium.Demo.Informes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Seleccion_Compras_GraficaMaster_WPF : UserControl
    {
        public UsuarioActivo _sesion;
        private InventariosExistenciasViewModel _MiViewModel;

        private int Id_CriterioAgrupacion;
        private int Id_Valor;
        private int Id_Anio;
    
        public int Id_Articulo;
        private int Id_Proveedor;

        private int Id_Linea;
        private int Id_Color;
        private int Id_GrupoProducto;
        private int Id_Talla;
        private int Id_Subgrupo;
        private int Id_Almacen;

        private int Id_GrupoProveedores;
        private int Id_Colaborador;

        private int Id_TipoVenta;

        private string Id_Top;

        private int Id_M01;
        private int Id_M02;
        private int Id_M03;
        private int Id_M04;
        private int Id_M05;
        private int Id_M06;
        private int Id_M07;
        private int Id_M08;
        private int Id_M09;
        private int Id_M10;
        private int Id_M11;
        private int Id_M12;
        bool IsReportEmbedded;

        public Seleccion_Compras_GraficaMaster_WPF(ref UsuarioActivo usuario, bool IsReportEmbedded = false)
        {
            this._sesion = usuario;
            Init();
            

            InitializeComponent();
            _MiViewModel = (InventariosExistenciasViewModel)this.DataContext;
            
            
            CargarCombos();
            
            

            CargarDatos();

            if (IsReportEmbedded)
                ButtonRegresar.Visibility = System.Windows.Visibility.Hidden;
            this.IsReportEmbedded = IsReportEmbedded;
        }

        public Seleccion_Compras_GraficaMaster_WPF()
        {
            
            Init();


            InitializeComponent();
            _MiViewModel = (InventariosExistenciasViewModel)this.DataContext;

        }

        public void CargarDatos()
        {
            if (PartidasDataGrid != null && _MiViewModel != null)
            {
                SIIE_ERP.BaseDatos.Compras.Transacciones.Documento_Compras MiDocumento_Compras = new SIIE_ERP.BaseDatos.Compras.Transacciones.Documento_Compras(_sesion.RFC, _sesion.UserName);
                DataTable dt_Datos = MiDocumento_Compras.Get_Report_GraficaMaster(Id_CriterioAgrupacion, Id_Valor, Id_Articulo, Id_Almacen, Id_GrupoProducto, Id_Subgrupo, Id_Linea, Id_Talla, Id_Color, Id_Colaborador, Id_GrupoProveedores, Id_Proveedor, Id_Anio, Id_M01, Id_M02, Id_M03, Id_M04, Id_M05, Id_M06, Id_M07, Id_M08, Id_M09, Id_M10, Id_M11, Id_M12, Id_TipoVenta, Id_Top);

                PartidasDataGrid.Items.Clear();
                PartidasDataGrid_Resumen.Items.Clear();

                for (int i = 0; i < dt_Datos.Rows.Count; i++)
                {
                    PartidasDataGrid.Items.Add(new TestClass() { Grupo = dt_Datos.Rows[i]["Grupo"].ToString(), Valor = Double.Parse(dt_Datos.Rows[i]["Valor"].ToString()) });
                }

                double? LastValue = null;
                int? position = null;
                int ContadorTies = 0;

                for (int i = 0; i < dt_Datos.Rows.Count; i++)
                {
                    if (ContadorTies < 5)
                    {
                        if (!(LastValue == null))
                        {
                            if (!(Double.Parse(dt_Datos.Rows[i]["Valor"].ToString()) == LastValue))
                            {
                                ContadorTies++;
                                LastValue = Double.Parse(dt_Datos.Rows[i]["Valor"].ToString());
                            }
                        }
                        else
                        {
                            ContadorTies++;
                            LastValue = Double.Parse(dt_Datos.Rows[i]["Valor"].ToString());
                        }
                    }
                    else
                    {
                        if (position == null)
                        {
                            position = i;
                            dt_Datos.Rows[(int)position]["Grupo"] = "Otros";
                        }
                        else
                        {
                            dt_Datos.Rows[(int)position]["Valor"] = Double.Parse(dt_Datos.Rows[(int)position]["Grupo"].ToString()) + Double.Parse(dt_Datos.Rows[i]["Grupo"].ToString());
                        }
                    }
                }

                if (position != null)
                {
                    while (dt_Datos.Rows.Count > position + 1)
                    {
                        dt_Datos.Rows.RemoveAt(dt_Datos.Rows.Count - 1);
                    }
                }

                _MiViewModel.DatosGraph.Clear();

                for (int i = 0; i < dt_Datos.Rows.Count; i++)
                {
                    PartidasDataGrid_Resumen.Items.Add(new TestClass() { Grupo = dt_Datos.Rows[i]["Grupo"].ToString(), Valor = Double.Parse(dt_Datos.Rows[i]["Valor"].ToString()) });
                    _MiViewModel.DatosGraph.Add(new TestClass() { Grupo = dt_Datos.Rows[i]["Grupo"].ToString(), Valor = Double.Parse(dt_Datos.Rows[i]["Valor"].ToString()) });
                }
            }
        }

        public void Init()
        {
            Id_Articulo = -1;
            Id_Linea = -1;
            Id_GrupoProducto = -1;
            Id_CriterioAgrupacion = -1;
            Id_Valor = -1;
            Id_Almacen = -1;
            Id_Subgrupo = -1;
            Id_Color = -1;
            Id_Talla = -1;

            Id_Colaborador = -1;
            Id_GrupoProveedores = -1;
            Id_Proveedor = -1;

            Id_M01 = 1;
            Id_M02 = 2;
            Id_M03 = 3;
            Id_M04 = 4;
            Id_M05 = 5;
            Id_M06 = 6;
            Id_M07 = 7;
            Id_M08 = 8;
            Id_M09 = 9;
            Id_M10 = 10;
            Id_M11 = 11;
            Id_M12 = 12;

            Id_Top = "Top";
        }

        public void CargarCombos()
        {
            ComboBoxUI.ComboAnio(ref Combo_Anio);
            ComboBoxUI.ComboArticulos_AutoComplete_Inventarios(ref Combo_ArticulosAutoComplete, this._sesion, "");
            ComboBoxUI.ComboProveedores_AutoComplete(ref Combo_ProveedoresAutoComplete, this._sesion, "");
            ComboBoxUI.ComboLinea(Combo_Linea, this._sesion, IsForReport: true);
            ComboBoxUI.ComboGrupoProductos(Combo_GrupoProducto, this._sesion, IsForReport: true);
            ComboBoxUI.ComboAlmacen(ref Combo_Almacen, this._sesion, IsForReport: true);
            ComboBoxUI.ComboGrupoProveedores(ref Combo_GrupoProveedores, this._sesion, IsForReport: true);
            ComboBoxUI.ComboColaborador(ref Combo_Colaborador, this._sesion, IsForReport: true);
        }

        private void ButtonRegresar_Click(object sender, RoutedEventArgs e)
        {
            Viewbox Aux = (Viewbox)this.Parent;
            Aux.Child = null;
            Aux.Child = new Menu_Informes(ref _sesion);           
        }

        private void ButtonRegresar_Click_1(object sender, RoutedEventArgs e)
        {
            Viewbox Aux = (Viewbox)this.Parent;
            Aux.Child = null;
            Aux.Child = new Menu_Informes(ref _sesion);  
        }

        private void Combo_ArticulosAutoComplete_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_ArticulosAutoComplete.SelectedItem != null)
                Id_Articulo = Int32.Parse(((CAutocomplete)Combo_ArticulosAutoComplete.SelectedItem).Id);
            else
                Id_Articulo = -1;




            CargarDatos();
        }

        private void Combo_Linea_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_Linea.SelectedValue != null)
            {
                Id_Linea = Int32.Parse(((CItem)Combo_Linea.SelectedItem).Id);

                var str = Combo_Linea.SelectedValue.ToString();
                ComboBoxUI.ComboColor(ref Combo_Color, _sesion, Int32.Parse(str), IsForReport: true);
                ComboBoxUI.ComboTalla(ref Combo_Talla, _sesion, Int32.Parse(str), IsForReport: true);




                CargarDatos();
            }
        }

        private void Combo_GrupoProducto_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_GrupoProducto.SelectedValue != null)
            {
                Id_GrupoProducto = Int32.Parse(((CItem)Combo_GrupoProducto.SelectedItem).Id);

                var str = Combo_GrupoProducto.SelectedValue.ToString();
                ComboBoxUI.ComboSubGrupoProductos(ref Combo_Subgrupo, _sesion, Int32.Parse(str), IsForReport: true);




                CargarDatos();
            }
        }

        private void Combo_CriterioAgrupacion_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_CriterioAgrupacion = Int32.Parse(((ComboBoxItem)Combo_CriterioAgrupacion.SelectedItem).Tag.ToString());



            CargarDatos();
        }

        private void Combo_Valor_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_Valor = Int32.Parse(((ComboBoxItem)Combo_Valor.SelectedItem).Tag.ToString());



            CargarDatos();
        }

        private void Combo_Almacen_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_Almacen = Int32.Parse(((CItem)Combo_Almacen.SelectedItem).Id);




            CargarDatos();
        }

        private void Combo_Subgrupo_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_Subgrupo.SelectedValue != null)
            {
                Id_Subgrupo = Int32.Parse(((CItem)Combo_Subgrupo.SelectedItem).Id);



                CargarDatos();
            }
        }

        private void Combo_Color_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_Color.SelectedValue != null)
            {
                Id_Color = Int32.Parse(((CItem)Combo_Color.SelectedItem).Id);



                CargarDatos();
            }
        }

        private void Combo_Talla_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_Talla.SelectedValue != null)
            {
                Id_Talla = Int32.Parse(((CItem)Combo_Talla.SelectedItem).Id);




                CargarDatos();
            }
        }

        private void Combo_Anio_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_Anio = Int32.Parse(((CItem)Combo_Anio.SelectedItem).Id);

            


            CargarDatos();
        }

        private void Combo_ClientesAutoComplete_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_ProveedoresAutoComplete.SelectedItem != null)
                Id_Proveedor = Int32.Parse(((CAutocomplete)Combo_ProveedoresAutoComplete.SelectedItem).Id);
            else
                Id_Proveedor = -1;




            CargarDatos();
        }

        private void Combo_GrupoCliente_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_GrupoProveedores = Int32.Parse(((CItem)Combo_GrupoProveedores.SelectedItem).Id);



            CargarDatos();
        }

        private void Combo_Colaborador_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_Colaborador = Int32.Parse(((CItem)Combo_Colaborador.SelectedItem).Id);




            CargarDatos();
        }

        private void Combo_TipoVenta_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_TipoVenta = Int32.Parse(((ComboBoxItem)Combo_TipoVenta.SelectedItem).Tag.ToString());        		



            CargarDatos();
        }

        private void Mes_1_Checked_1(object sender, RoutedEventArgs e)
        {
            if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nEne IsChecked:True"))
                Id_M01 = 1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nFeb IsChecked:True"))
                Id_M02 = 2;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nMar IsChecked:True"))
                Id_M03 = 3;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nAbr IsChecked:True"))
                Id_M04 = 4;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nMay IsChecked:True"))
                Id_M05 = 5;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nJun IsChecked:True"))
                Id_M06 = 6;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nJul IsChecked:True"))
                Id_M07 = 7;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nAgo IsChecked:True"))
                Id_M08 = 8;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nSep IsChecked:True"))
                Id_M09 = 9;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nOct IsChecked:True"))
                Id_M10 = 10;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nNov IsChecked:True"))
                Id_M11 = 11;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nDic IsChecked:True"))
                Id_M12 = 12;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nEne IsChecked:False"))
                Id_M01 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nFeb IsChecked:False"))
                Id_M02 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nMar IsChecked:False"))
                Id_M03 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nAbr IsChecked:False"))
                Id_M04 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nMay IsChecked:False"))
                Id_M05 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nJun IsChecked:False"))
                Id_M06 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nJul IsChecked:False"))
                Id_M07 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nAgo IsChecked:False"))
                Id_M08 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nSep IsChecked:False"))
                Id_M09 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nOct IsChecked:False"))
                Id_M10 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nNov IsChecked:False"))
                Id_M11 = -1;
            else if (sender.ToString().Equals("System.Windows.Controls.CheckBox Content:\r\nDic IsChecked:False"))
                Id_M12 = -1;

            CargarDatos();
        }

        private void Button_DeseleccionProveedor_Click_1(object sender, RoutedEventArgs e)
        {
            Combo_ProveedoresAutoComplete.SelectedItem = null;
        }

        private void Button_DeseleccionArticulo_Click_1(object sender, RoutedEventArgs e)
        {
            Combo_ArticulosAutoComplete.SelectedItem = null;
        }

        private void Combo_Top_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Id_Top = ((ComboBoxItem)Combo_Top.SelectedItem).Tag.ToString();
            CargarDatos();
        }
    }
}

